package com.csr.csrcheck.service.impl;


import com.csr.csrcheck.mapper.ProductMapper;
import com.csr.csrcheck.pojo.*;
import com.csr.csrcheck.service.ProductService;
import com.csr.csrcheck.service.ex.CompanyException;
import com.csr.csrcheck.service.ex.ProductException;
import com.csr.csrcheck.util.PageResult;
import com.csr.csrcheck.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductMapper productMapper;

    public List<Product> getProductlist() {
        List<Product> list = this.productMapper.getProductlist();
        if (list == null)
            throw new ProductException("");
        return list;
    }

    public List<Product> getProductlistbyname(String product_name) {
        List<Product> list = this.productMapper.getProductlistbyname(product_name);
        if (list == null)
            throw new ProductException("");
        return list;
    }

    public PageResult listpage(int pageNum, int pageSize, String product_name) {
        return PageUtils.getPageResult(pageNum, pageSize, getpageinfo(pageNum, pageSize, product_name));
    }

    public List<Product> list() {
        List<Product> list = this.productMapper.list();
        if (list == null)
            throw new CompanyException("");
        return list;
    }

    private PageInfo getpageinfo(int pageNum, int pageSize, String product_name) {
        PageHelper.startPage(pageNum, pageSize);
        List<Product> list = this.productMapper.listpage(product_name);
        if (list == null)
            throw new CompanyException("");
        return new PageInfo(list);
    }

    public List<Product_type> selectProduct_type() {
        return this.productMapper.selectProduct_type();
    }

    public List<Patent_type> selectPatent() {
        return this.productMapper.selectPatent();
    }

    public List<Company> selectCompany() {
        return this.productMapper.selectCompany();
    }

    public List<Stage> selectStage() {
        return this.productMapper.selectStage();
    }

    public int addproduct(Product product) {
        return this.productMapper.addproduct(product);
    }
}
